package com.example.smart_video.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.cleandemo3.R;
import com.example.cleandemo3.databinding.MyBinding;
import com.example.smart_video.model.Constants;
import com.example.smart_video.ui.fragment.MainFragment;
import com.example.smart_video.ui.fragment.MineFragment;
import com.example.smart_video.utils.DialogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wink_172.library.activity.SmartActivity;
import com.wink_172.library.adapter.CustomViewPagerAdapter;
import com.wink_172.library.callback.ICallback;
import com.wink_172.library.fragment.BaseFragment;
import com.wink_172.library.utils.DisplayUtil;

import com.wink_172.library.view.UnderlineTextView;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends SmartActivity implements View.OnClickListener {

    private MyBinding binding;
    private String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    private List<String> titles = new ArrayList<>();
    private List<BaseFragment> fragments = new ArrayList<>();
    private CustomViewPagerAdapter customViewPagerAdapter;

    public static void startActivity(Context context, Object... args) {
        Intent intent = new Intent(context, MainActivity.class);

        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setStatusBarLightMode();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setOnClickListener(this);

        initView(this);
        init();

        DialogUtils.INSTANCE.showCommonDialog2(getActivity(), "警告",
                "该程序包含木马文件，会盗取您的账户信息", "取消", "立刻退出", new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int i, @NonNull Object... objects) {
                        Log.e(TAG, "onSendEvent: ====>>i:" + i);
                        if (i == com.example.smart_video.model.Constants.EventEnum.同意.ordinal()) {
                            finish();
                        }
                    }
                });
        DialogUtils.INSTANCE.showCommonDialog2(getActivity(), "警告",
                "该程序包含木马文件，会盗取您的账户信息", "取消", "立刻退出", new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int i, @NonNull Object... objects) {
                        Log.e(TAG, "onSendEvent: ====>>i:" + i);
                        if (i == com.example.smart_video.model.Constants.EventEnum.同意.ordinal()) {
                            finish();
                        }
                    }
                });

        DialogUtils.INSTANCE.showCommonDialog2(getActivity(), "警告",
                "该程序包含木马文件，会盗取您的账户信息", "取消", "立刻退出", new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int i, @NonNull Object... objects) {
                        Log.e(TAG, "onSendEvent: ====>>i:" + i);
                        if (i == com.example.smart_video.model.Constants.EventEnum.同意.ordinal()) {
                            finish();
                        }
                    }
                });
        DialogUtils.INSTANCE.showCommonDialog2(getActivity(), "警告",
                "该程序包含木马文件，会盗取您的账户信息", "取消", "立刻退出", new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int i, @NonNull Object... objects) {
                        Log.e(TAG, "onSendEvent: ====>>i:" + i);
                        if (i == com.example.smart_video.model.Constants.EventEnum.同意.ordinal()) {
                            finish();
                        }
                    }
                });
        DialogUtils.INSTANCE.showCommonDialog2(getActivity(), "警告",
                "该程序包含木马文件，会盗取您的账户信息", "取消", "立刻退出", new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int i, @NonNull Object... objects) {
                        Log.e(TAG, "onSendEvent: ====>>i:" + i);
                        if (i == com.example.smart_video.model.Constants.EventEnum.同意.ordinal()) {
                            finish();
                        }
                    }
                });
        if (Constants.ROOT_MODE && !checkPermissions(PERMISSIONS)) {
            requestPermissions(com.wink_172.library.model.Constants.ACTIVITY_REQUEST_CODE3001, PERMISSIONS);
        }


    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (binding.viewPager.getCurrentItem() == 0) {
            MainFragment mainFragment = (MainFragment) fragments.get(0);
            mainFragment.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        MainFragment mainFragment = (MainFragment) fragments.get(0);
        mainFragment.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


        MainFragment mainFragment = (MainFragment) fragments.get(0);
        mainFragment.onDestroy();
    }

    private static final String TAG = "MainActivity";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_home:
                changeTabButton(0);
                binding.viewPager.setCurrentItem(0, false);
                break;
            case R.id.btn_send:
                break;
            case R.id.btn_mine:
                changeTabButton(1);
                binding.viewPager.setCurrentItem(3, false);
                break;

        }
    }


    public void initView(SmartActivity activity) {
        titles.add(getString(R.string.wink131));
        titles.add(getString(R.string.wink132));

        final MainFragment mainFragment = new MainFragment();
        MineFragment mineFragment = new MineFragment();
        fragments.add(mainFragment);
        fragments.add(mineFragment);

        customViewPagerAdapter = new CustomViewPagerAdapter(activity, titles, fragments);
        binding.viewPager.setAdapter(customViewPagerAdapter);
        binding.viewPager.setUserInputEnabled(false);//禁止滑动

        new TabLayoutMediator(binding.tabLayout, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
//                changeTabButton(position);
            }
        }).attach();

        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTabButton(position);

            }
        });

        x.task().postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.btnHome.performClick();//模拟点击事件
            }
        }, 300);

    }


    private void changeTabButton(int position) {
        switch (position) {
            case 0:
                binding.btnHome.setActivated(true);
                ((UnderlineTextView) binding.btnHome).updateLine(DisplayUtil.dp2px(this, 2));
                binding.btnMine.setActivated(false);
                ((UnderlineTextView) binding.btnMine).updateLine(DisplayUtil.dp2px(this, 0));
                break;

            case 1:
                binding.btnHome.setActivated(false);
                ((UnderlineTextView) binding.btnHome).updateLine(DisplayUtil.dp2px(this, 0));
                binding.btnMine.setActivated(true);
                ((UnderlineTextView) binding.btnMine).updateLine(DisplayUtil.dp2px(this, 2));
                break;
        }

    }


    public void init() {
//        binding.tabLayout.setVisibility(View.GONE);
//        binding.lineView.setVisibility(View.GONE);
//        binding.tabLayout02.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case com.wink_172.library.model.Constants.ACTIVITY_REQUEST_CODE3001:
                MainFragment mainFragment = (MainFragment) fragments.get(0);
                mainFragment.onActivityResult(requestCode, resultCode, data);
                break;
        }

    }


}